<?php
import('Com.Auth');
import('ORG.Util.Page'); //导入分页类
import("ORG.Net.UploadFile");
class CourseAction extends Action
{
	public function _initialize(){//传值
		$this->assign('no',Auth::getno());
    	$this->assign('name',Auth::getname());
    	$this->assign('role',Auth::getrole());
	}
	public function index(){//所有院系
		$c=M('department');
		$p=new Page($c->count(), 20);
		$list=$c->limit($p->firstRow.','.$p->listRows)->select();
		$this->assign('list',$list); // 模板发量赋值
		$this->assign ( 'page', $p->show() );
		$this->display(); // 输出模板
	}
	public function allteacher(){//显示所有老师
		$c=M('department');
		$p=new Page($c->count(), 20);
		$list=$c->limit($p->firstRow.','.$p->listRows)->select();
		$this->assign('list',$list); // 模板发量赋值
		$this->assign ( 'page', $p->show() );
		$this->display(); // 输出模板
	}
	public function teacher(){//显示院系所有老师
		$dname=D('department')->where('id='.$_GET['id'])->getfield('name');
		$c=M('info');
		$map['departmentid']=$_GET['id'];
		$map['role']='教师';
		$list=$c->where($map)->select();
		$count=$c->where($map)->count();
		D('department')->where('id='.$_GET['id'])->setfield('people',$count);
		$this->assign('list',$list); // 模板发量赋值
		$this->assign ('count',$count);
		$this->assign ('dname',$dname);
		//dump($_GET['id']);dump($list);dump($count);dump($dname);
		$this->display(); // 输出模板
	}
	public function department(){//院系的所有课程
		$c=M('course');
		$p=new Page($c->count(), 20);
		$list=$c->where('departmentid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->select();
		$this->assign('list',$list); // 模板发量赋值
		$this->assign ( 'page', $p->show() );
		//$custom=D('custom_detail');
		//$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		//$map['courseid']=$_GET['id'];
		//$map['id']=$_GET['p'];
		//$a=$custom->where($map)->select();
		$c = M('department')->find($_GET['id']);
		//$this->assign('a',$a);
		//$this->assign('id',$_GET['p']);
		//$this->assign('clist',$clist);
		$this->assign('departmentid',$_GET['id']);
		//$this->assign('courseid',$_GET['p']);
		$this->assign('dname',$c['name']); // 模板发量赋值
		$this->display(); // 输出模板
	}
	public function courseall(){//所有课程
		$form=D('department');
		$list=$form->select();
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function course_all(){//所有课程
		$dname=D('department')->where('id='.$_GET['id'])->getfield('name');
		$form=D('course_detail');
		$list=$form->where('departmentid='.$_GET['id'])->select();
		$this->assign('dname',$dname);
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function coursedetail_jp(){//精品课程
		$form=D('course_detail');
		$list=$form->where('excellent=1')->order('pv desc')->select();
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function coursedetail_all(){//所有课程
		$form=D('course_detail');
		$list=$form->order('pv desc')->select();
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function course(){//中间跳转页面
		$form=D('course');
		$form->setInc('pv','id='.$_GET['id'],1,1);		
		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(10)->select();
		$map['courseid']=$_GET['id'];
		$map['id']=$_GET['p'];
		$a=$custom->where($map)->select();
		$c = M('course')->find($_GET['id']);
		if ($c['choose']==1)
		$c['url']='__APP__/'.$c['url'];
		$this->assign('a',$a);
		$this->assign('id',$_GET['p']);
		$this->assign('clist',$clist);
		$this->assign('courseid',$_GET['id']);
		$this->assign('coursename',$c['name']); // 模板发量赋值
		$this->assign('url',$c['url']);
		$this->display();
	}
	public function custom(){//课程主页
		$b=M('course_detail')->find($_GET['id']);
		$form=D('custom');
		$form->setInc('pv','id='.$_GET['p'],1,1);
		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		$map['courseid']=$_GET['id'];
		$map['id']=$_GET['p'];
		$a=$custom->where($map)->select();
		$c = M('course')->find($_GET['id']);
		$this->assign('a',$a);
		$this->assign('id',$_GET['p']);
		$this->assign('clist',$clist);
		$this->assign('courseid',$_GET['id']);
		$this->assign('departmentid',$b['departmentid']);
		$this->assign('dname',$b['dname']);
		$this->assign('coursename',$c['name']); // 模板发量赋值
		$this->display();
	}
	public function custom1(){//院系主页
		if(Auth::getrole()=='校级管理员')
				$b=1;//是否显示删除
			elseif (Auth::getrole()=='院系管理员'){
				if (Auth::getdepartment()==$_GET['id'])
					$b=1;//是否显示删除
			}	
		else 
			$a=false;//是否显示删除
		$form=D('custom1');
		$form->setInc('pv','departmentid='.$_GET['id'],1,1);
		$custom1=D('custom1_detail');
		$a=$custom1->getByDepartmentid($_GET['id']);
		$c = M('department')->find($_GET['id']);
		$count=M('course')->where('departmentid='.$_GET['id'])->count();
		$this->assign('a',$a);
		$this->assign('b',$b);
		$this->assign('count',$count);
		//$this->assign('id',$_GET['p']);
		$this->assign('departmentid',$_GET['id']);
		$this->assign('dname',$c['name']); // 模板发量赋值
		$this->display();
	}
	public function discuss()//讨论区
	{	
		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		$this->assign('clist',$clist);
		$discuss=D('discuss_detail');
		$courseid=$_GET['id'];
		if (!empty($_GET['id'])){
			$map['courseid']=array('eq',$_GET['id']);
			$count=$discuss->where($map)->count();
			$p=new Page($count, 20);
			$list=$discuss->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			if(Auth::getrole()=='校级管理员')
				$a=true;//是否显示删除
			elseif (Auth::getrole()=='院系管理员'){
				$course=D('course');
				$departmentid=$course->where('id='.$_GET['id'])->getfield(departmentid);
				if (Auth::getdepartment()==$departmentid)
					$a=true;//是否显示删除
			}	
			elseif (Auth::getrole()=='教师'){
				$form=D('tcrelate');
				$no=$form->where('courseid='.$_GET['id'])->getfield(no);
				if (Auth::getno()==$no)
					$a=true;//是否显示删除
			}
		else 
			$a=false;//是否显示删除
		}else{ 
           exit('课程不存在');
        } 
        $c = M('course')->find($courseid);
        $page = $p->show ();
		$this->assign ( 'a', $a );
		$this->assign ( 'no',Auth::getno() );
		$this->assign ( 'coursename', $c['name'] );
		$this->assign ( 'courseid', $courseid );
		$this->assign ( 'page', $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->display(); // 输出模板
	}
	public function discussadd()//发帖
	{	
		$discuss = D('discuss'); // 实例化模型类
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['pv']=0;
		$_POST['count']=1;
		$_POST['no']=Auth::getno();
		if($discuss->create()){
			if($discuss->add())
				$this->success('表单提交成功');
			else
				$this->error('数据库插入错误');
		}
		$this->error($discuss->getError());
		
	}
	public function discussdelete()//删帖
	{	
		if(Auth::getrole()!='访客'&&Auth::getrole()!='学生'){
		$id=$_POST['id'];
		$response=D('response');
		$response->where('discussid='.$_POST['id'])->delete();
		$discuss=D('discuss');
		$discuss->delete($id);
		$this->redirect('discuss',array('id'=>$_POST['courseid']));
		}
		elseif (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else 
			exit('权限不够');
	}
	public function discusstopic()//帖子
	{	
		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		$this->assign('clist',$clist);
		if(!empty($_GET['id'])) { 
			$discuss=D('discuss');
			$discuss->setInc('pv','id='.$_GET['id'],1,1);
			$discuss=D('discuss_detail'); 
            $vo=$discuss->getByid($_GET['id']); 
            $response=D('response_detail');
            $count = $response->where('discussid='.$_GET['id'])->count();    //计算总数 
	        $p = new Page ( $count, 20 ); 
	    	$list=$response->where('discussid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->order('id')->select();
            //$list=$response->where('discussid='.$_GET['id'])->select();
            if(vo){ 
            	if(Auth::getrole()=='校级管理员')
				$a=true;//是否显示删除
				elseif (Auth::getrole()=='院系管理员'){
				$course=D('course');
				$departmentid=$course->where('id='.$_GET['id'])->getfield(departmentid);
				if (Auth::getdepartment()==$departmentid)
				$a=true;//是否显示删除
			}	
			elseif (Auth::getrole()=='教师'){
			$form=D('tcrelate');
			$no=$form->where('courseid='.$_GET['id'])->getfield(no);
			if (Auth::getno()==$no)
				$a=true;//是否显示删除
		}
		else 
			$a=false;//是否显示删除
			$d = M('discuss')->find($_GET['id']);
			$c = M('course')->find($d['courseid']);
			$this->assign('no',Auth::getno());
			$this->assign ( 'coursename', $c['name']);
			$this->assign ( 'courseid', $c['id']);
			$this->assign ( 'a', $a );
			$this->assign ( 'vo', $vo );
			$page = $p->show ();
			$this->assign ( 'page', $page );
            $this->assign('list',$list);
            $this->assign('role',Auth::getrole());
            $this->display(); 
            }else{ 
                exit('不存在！'); 
            } 
        }else{ 
            exit('不存在！'); 
        } 
	}
	public function responseadd()//回复
	{
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		$discuss=D('discuss');
		//$discuss->setInc('count','id='.$_POST['discussid'],1,1);
		$count=$discuss->where('id='.$_POST['discussid'])->getfield(count)+1;
		$response = D('response'); // 实例化模型类	
		$_POST['no']=Auth::getno();
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['count']=$count;
		if($response->Create()){ // 创建数据对象	
			$response->add(); // 写入数据库
			$discuss->setInc('count','id='.$_POST['discussid'],1,1);
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($response->getError(),1);
		}
		//$this->redirect('discusstopic',array('id'=>$_POST['discussid'])); // 成功后重定向到操作页面
	}
	public function responsedelete()//删回复
	{	
		if(Auth::getrole()!='访客'&&Auth::getrole()!='学生'){
		$id=$_POST['id'];
		$response=D('response');
		$response->delete($id);
		$this->redirect('discusstopic',array('id'=>$_POST['discussid']));
		}
		elseif (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else 
			exit('权限不够');
	}
public function upload() {//上传
		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		$this->assign('clist',$clist);
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if(Auth::getrole()=='学生')
		exit('权限不够');
		if (!empty($_GET['id'])){
			$c=m('course')->find($_GET['id']);
	   		$this->assign('courseid',$_GET['id']);
	   		$this->assign('coursename',$c['name']);
	   		$this->display();
		}
		else 
			exit('课程不存在');
        if (!empty($_FILES)) { 
            //如果有文件上传 上传附件 
            $this->_upload(); 
            //$this->forward(); 
        }
        
    } 
	public function _upload() {//上传
        $id=$_POST['id'];
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize=-1;
		if (!file_exists('./File/course/'.$id.'/'))
			mkdir('./File/course/'.$id.'/');
        $upload->savePath ='./File/course/'.$id.'/';  
      	$upload->allowExts=array(); 
      	$upload->saveRule=time;
		if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $_POST['name'] = $uploadList[0]['name'];
            $_POST['savename'] = $uploadList[0]['savename'];
            $_POST['path'] = $uploadList[0]['savepath'];
            $_POST['size'] = $uploadList[0]['size'];
        $model = M('file');
        //保存当前数据对象
        $data['no']=Auth::getno();
        $data['name'] = $_POST['name'];
        $data['savename'] = $_POST['savename'];
        $data['size']= $_POST['size'];
        $data['path']= substr ($_POST['path'], 1);
        //$data['path']= $_POST['path'];
       	$data['downloadno']= 0;
        $data['courseid']= $_POST['id'];
        $data['time'] = date('Y-m-d H:i:s');
        $list = $model->add($data);
        $this->redirect('download',array('id'=>$_POST['id']));
        }  
   }
   public function download()//下载
   {	
   		$custom=D('custom_detail');
		$clist=$custom->where('courseid='.$_GET['id'])->limit(5)->select();
		$this->assign('clist',$clist);
   		if (!empty($_GET['id'])){
   			$courseid=$_GET['id'];
	   		$form=D('download');
	   		$map['courseid']=array('eq',$_GET['id']);
			$count=$form->where($map)->count();
			$p=new Page($count, 20);
			$list=$form->where($map)->limit($p->firstRow.','.$p->listRows)->order('id desc')->select();
   		if(Auth::getrole()=='校级管理员')
			$a=true;//是否显示删除
		elseif (Auth::getrole()=='院系管理员'){
			$course=D('course');
			$departmentid=$course->where('id='.$_GET['id'])->getfield(departmentid);
			if (Auth::getdepartment()==$departmentid)
				$a=true;//是否显示删除
		}	
		elseif (Auth::getrole()=='教师'){
			$form=D('tcrelate');
			$no=$form->where('courseid='.$_GET['id'])->getfield(no);
			if (Auth::getno()==$no)
				$a=true;//是否显示删除
		}
		else 
			$a=false;//是否显示删除
		}
   		else 
   			exit('课程不存在');
   		$c=m('course')->find($_GET['id']);
   		$page = $p->show ();
   		$this->assign ( 'page', $page );
   		$this->assign('a',$a);
   		$this->assign('courseid',$_GET['id']);
   		$this->assign('coursename',$c['name']);
   		$this->assign('list',$list);
   		$this->display(); // 输出模板
   }
   public function fileedit()//上传文件编辑
   {
   	if(!empty($_GET['id'])) { 
            $download=D("download"); 
            $vo=$download->getByid($_GET['id']); 
            if($vo) { 
            	$this->assign('id',$vo['courseid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
   }
    public function fileupdate() {//上传文件更新
    	$file=D("file");
    	if($file->create()){
    		$file->save(); 
    		$this->success('保存成功',1);
    	}
       	else{
       		$this->error($file->getError(),1);
       	}
        $this->redirect('download',array('id'=>$_POST['courseid']));        
    } 
    public  function filedelete(){//上传文件删除
		$id=$_POST['id'];
		$file=D("file");
		$a=$file->where('id='.$_POST['id'])->getfield(savename);
		@unlink('./File/course/1/'.$a);
		$file->delete($id);
		$this->redirect('download',array('id'=>$_POST['courseid']));
    } 
}
?>